﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.QuanTri
{
    public partial class QLSuaChua : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPhongHoc();
                Pager1.CurrentIndex = 1;
                BindHoatDong(Pager1.CurrentIndex);
                BindCatalog.BindLoaiTinhTrang(cboLoaiTinhTrang);
                BindCatalog.BindCanBo(cboCanBo);
                BindCatalog.BindCanBo(cboCanBoQuanLy);
            }
        }

        private void BindHoatDong(int PageNo)
        {
            int totalRow = 0;
            string whereClause = "";
            switch (H_PhongHocId.Value)
            {
                case "0":
                    whereClause += "MaThietBi in (Select MaThietBi from ThietBi)";
                    break;
                default:
                    whereClause += "MaThietBi in (Select MaThietBi from ThietBi where (MaPhong= " + H_PhongHocId.Value + "))";
                    break;
            }
            DataSet dsHoatDong = DataRepository.Provider.ExecuteDataSet("HoatDongSuaChua_GetPaged", whereClause, "NgayCapNhat DESC",
                PageNo - 1, Pager1.PageSize);
            rpHoatDong.DataSource = dsHoatDong.Tables[0];
            rpHoatDong.DataBind();
            try
            {
                totalRow = Convert.ToInt32(dsHoatDong.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = totalRow;
            if (totalRow <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            BindHoatDong(currnetPageIndex);
        }

        protected void cmdSave_OnClick(object sender, EventArgs e)
        {
            chuyenDoi(txtday1.Text + "," + txtday2.Text + "," + txtday3.Text + "," + txtday4.Text, litday1.Text + "," + litday2.Text + "," + litday3.Text + "," + litday4.Text);//chuỗi 2 sẽ bằng chuỗi 1
            BindThietBi(H_PhongHocId.Value);
        }
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        public void chuyenDoi(string str1, string str2)
        {
            string test = "";
            str1 = str1.Replace("\"", "");
            str1 = str1.Replace("{", "");
            str1 = str1.Replace("}", "");
            str1 = str1.Replace("[", "");
            str1 = str1.Replace("]", "");
            str1 = str1.Replace("id:", "");
            string[] arrayStr1 = str1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);//Vị trí
            string[] arrayStr2 = str2.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);//Mã thiết bị
            if (str1.Length > 0)
            {
                for (int i = 0; i <= arrayStr1.Length - 1; i++)
                {

                    string query = string.Empty;

                    conn.Open();
                    query = "UPDATE ThietBi SET ViTri = '" + arrayStr2[i] + "' where MaThietBi = '" + arrayStr1[i] + "'";
                    SqlCommand sqlCommand = new SqlCommand();
                    sqlCommand.Connection = conn;
                    sqlCommand.CommandText = query;
                    sqlCommand.ExecuteNonQuery();
                    conn.Close();

                }
            }
        }


        protected void rpPhongHoc_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                H_PhongHocId.Value = id.ToString();

                litPhong.Text = ": " + DataRepository.PhongHocProvider.GetByMaPhong(id).TenPhong;
                //litPhong2.Text = litPhong.Text;
                BindThietBi(H_PhongHocId.Value);
                BindHoatDong(Pager1.CurrentIndex);
                BindPhongHoc();
                cmdLuu.Visible = true;
                cmdReport.Visible = false;
            }
        }

        #region Hiển thị danh sách các thiết bị
        private void BindThietBi(string maPhong)
        {
            string whereClause = "";
            int totalRow = 0;
            if (maPhong == "0")
            {

                H_PhongHocId.Value = "1";
                maPhong = H_PhongHocId.Value;
            }
            whereClause = "MaPhong=" + maPhong;
            TList<ThietBi> TList_ThietBi = DataRepository.ThietBiProvider.GetPaged(whereClause, "ViTri DESC", 0, 0, out totalRow);
            litday1.Text = "";
            litday2.Text = "";
            litday3.Text = "";
            litday4.Text = "";
            TList<ThietBi> TList_ThietBi1 = new TList<ThietBi>();
            for (int i = 0; i < 10; i++)
            {
                TList_ThietBi1.Add(TList_ThietBi[i]);

                if (i == 9)
                {
                    litday1.Text += TList_ThietBi[i].ViTri;
                }
                else
                {
                    litday1.Text += TList_ThietBi[i].ViTri + ",";
                }
            }
            rpDay1.DataSource = TList_ThietBi1;
            rpDay1.DataBind();
            TList<ThietBi> TList_ThietBi2 = new TList<ThietBi>();
            for (int i = 10; i < 20; i++)
            {
                TList_ThietBi2.Add(TList_ThietBi[i]);
                if (i == 19)
                {
                    litday2.Text += TList_ThietBi[i].ViTri;
                }
                else
                {
                    litday2.Text += TList_ThietBi[i].ViTri + ",";
                }
            }
            rpDay2.DataSource = TList_ThietBi2;
            rpDay2.DataBind();
            TList<ThietBi> TList_ThietBi3 = new TList<ThietBi>();
            for (int i = 20; i < 30; i++)
            {
                TList_ThietBi3.Add(TList_ThietBi[i]);
                if (i == 29)
                {
                    litday3.Text += TList_ThietBi[i].ViTri;
                }
                else
                {
                    litday3.Text += TList_ThietBi[i].ViTri + ",";
                }
            }
            rpDay3.DataSource = TList_ThietBi3;
            rpDay3.DataBind();
            TList<ThietBi> TList_ThietBi4 = new TList<ThietBi>();
            for (int i = 30; i < 40; i++)
            {
                TList_ThietBi4.Add(TList_ThietBi[i]);
                if (i == 39)
                {
                    litday4.Text += TList_ThietBi[i].ViTri;
                }
                else
                {
                    litday4.Text += TList_ThietBi[i].ViTri + ",";
                }
            }
            rpDay4.DataSource = TList_ThietBi4;
            rpDay4.DataBind();

        }
        #endregion Hiển thị danh sách các thiết bị

        private void BindPhongHoc()
        {
            TList<PhongHoc> TList_PhongHoc = DataRepository.PhongHocProvider.GetAll();
            rpPhongHoc.DataSource = TList_PhongHoc;
            rpPhongHoc.DataBind();
        }

        public string getTenCanBo(object ob)
        {
            string id = ob.ToString();
            return DataRepository.CanBoProvider.GetByMaCanBo(id).HoTen;

        }

        public string getTenThietBi(object ob)
        {
            string id = ob.ToString();
            return DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id)).TenThietBi;

        }

        public string getLoaiTinhTrang(object ob)
        {
            string id = ob.ToString();
            return DataRepository.LoaiTinhTrangProvider.GetByMaLoaiTinhTrang(Convert.ToInt32(id)).TenLoaiTinhTrang;

        }

        public string getTenPhong(object ob)
        {
            string id = ob.ToString();
            return DataRepository.PhongHocProvider.GetByMaPhong(DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id)).MaPhong).TenPhong;

        }

        public string getTinhTrang(object ob)
        {
            string id = ob.ToString();
            ThietBi ent_ThietBi = DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id));
            if (ent_ThietBi.TrangThai == true)
                return "<html><img src=../../Images/checkk.png /></html>";
            else
            {
                return "<html><img src=../../Images/redx.png /></html>";
            }

        }

        protected void rpHoatDong_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int id = Convert.ToInt32(e.CommandArgument);  //Mã hoạt động sửa chữa
                H_HoatDongSuaChua.Value = id.ToString();
                cmdReport.Visible = true;
                cmdLuu.Visible = false;
                HoatDongSuaChua ent_HoatDong = DataRepository.HoatDongSuaChuaProvider.GetByMaHoatDongSuaChua(id);
                H_NgayCapNhat.Value = ent_HoatDong.NgayCapNhat.ToString();
                H_MaThietBi.Value = ent_HoatDong.MaThietBi.ToString();//MaThietBi
                ThietBi ent_ThietBi = DataRepository.ThietBiProvider.GetByMaThietBi(ent_HoatDong.MaThietBi);
                PhongHoc ent_PhongHoc = DataRepository.PhongHocProvider.GetByMaPhong(ent_ThietBi.MaPhong);
                litCanBo.Text = "<font color=red>Quản lý " + ent_PhongHoc.TenPhong + ": <b>" + getTenCanBo(ent_PhongHoc.MaCanBo) + "</b></font>";
                cboLoaiTinhTrang.SelectedValue = ent_HoatDong.MaLoaiTinhTrang.ToString();      //LoaiTinhTrang
                txtGhiChu.Text = ent_HoatDong.GhiChu; //GhiChu
                cboCanBo.SelectedValue = ent_HoatDong.MaCanBo.ToString();   //MaCanBo
                chkTrangThai.Checked = ent_ThietBi.TrangThai;//TrangThai
                litTenThietBi.Text = "<font color=green>Thiết bị: <b>" + ent_ThietBi.TenThietBi + "</b></font>";
                ChiTietHoatDongSuaChua ent_CTHDSC =
                    DataRepository.ChiTietHoatDongSuaChuaProvider.GetByMaHoatDongSuaChua(id);
                if (ent_CTHDSC != null)
                {
                    txtHuongGiaiQuyet.Text = ent_CTHDSC.HuongGiaiQuyet;
                    cboCanBoQuanLy.SelectedValue = ent_CTHDSC.MaCanBoQuanLy;
                }
                BindHoatDong(Pager1.CurrentIndex);
            }
        }

        protected void cmdReport_OnClick(object sender, EventArgs e)
        {
            bool status = false;
            HoatDongSuaChua ent_SuaChua = new HoatDongSuaChua();
            ent_SuaChua.MaHoatDongSuaChua = Convert.ToInt32(H_HoatDongSuaChua.Value);
            ent_SuaChua.MaThietBi = Convert.ToInt32(H_MaThietBi.Value);
            ent_SuaChua.MaLoaiTinhTrang = Convert.ToInt32(cboLoaiTinhTrang.SelectedValue);
            ent_SuaChua.TrangThai = false;
            ent_SuaChua.NgayCapNhat = DateTime.ParseExact(Common.StandardString(Common.thayTheChuoiNgay(H_NgayCapNhat.Value)), "dd/MM/yyyy", CultureInfo.InvariantCulture);
            ent_SuaChua.GhiChu = txtGhiChu.Text;
            ent_SuaChua.MaCanBo = cboCanBo.SelectedValue;
            status = DataRepository.HoatDongSuaChuaProvider.Update(ent_SuaChua);
            if (!status)
            {
                WebMessage.Show("Báo cáo thất bại! Xem lại tiến trình!");
            }
            else
            {
                ChiTietHoatDongSuaChua ent_CTHDSC = new ChiTietHoatDongSuaChua();
                ent_CTHDSC.MaHoatDongSuaChua = ent_SuaChua.MaHoatDongSuaChua;
                if(cboCanBoQuanLy.SelectedValue!="")
                {
                    ent_CTHDSC.MaCanBoQuanLy = cboCanBo.SelectedValue;
                }
                ent_CTHDSC.NgaySuaChua = Common.GetServerDateTime();
                ent_CTHDSC.HuongGiaiQuyet = txtHuongGiaiQuyet.Text;
                if (
                    DataRepository.ChiTietHoatDongSuaChuaProvider.GetByMaHoatDongSuaChua(ent_SuaChua.MaHoatDongSuaChua) !=
                    null)
                {
                    DataRepository.ChiTietHoatDongSuaChuaProvider.Update(ent_CTHDSC);
                }
                else
                {
                    DataRepository.ChiTietHoatDongSuaChuaProvider.Insert(ent_CTHDSC);
                }
                //BindThietBi(cboPhongHoc.SelectedValue);
                string query = string.Empty;

                conn.Open();
                query = "UPDATE ThietBi SET TrangThai = " + (chkTrangThai.Checked == true ? "1" : "0") + " where MaThietBi = '" + H_MaThietBi.Value + "'";
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = conn;
                sqlCommand.CommandText = query;
                sqlCommand.ExecuteNonQuery();
                conn.Close();
                cboLoaiTinhTrang.SelectedValue = "0";
                txtGhiChu.Text = "";
                txtHuongGiaiQuyet.Text = "";
                chkTrangThai.Checked = false;
                cboCanBo.SelectedValue = "0";
                cmdReport.Visible = false;
                H_MaThietBi.Value = "";
                BindHoatDong(Pager1.CurrentIndex);
                BindThietBi(H_PhongHocId.Value);
                Page_Load(sender, e);
                WebMessage.Show("Cập nhật thông tin thành công!");
            }
        }

        public string getHuongGiaiQuyen(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            ChiTietHoatDongSuaChua ent_ChiTiet = DataRepository.ChiTietHoatDongSuaChuaProvider.GetByMaHoatDongSuaChua(id);
            if (ent_ChiTiet != null)
            {
                return ent_ChiTiet.HuongGiaiQuyet;
            }
            else
            {
                return "Chờ cập nhật";
            }
        }
        public string getNgaySuaChua(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            ChiTietHoatDongSuaChua ent_ChiTiet = DataRepository.ChiTietHoatDongSuaChuaProvider.GetByMaHoatDongSuaChua(id);
            if (ent_ChiTiet != null)
            {
                if (ent_ChiTiet.NgaySuaChua.ToString() == "NULL" || ent_ChiTiet.NgaySuaChua.ToString() == "")
                {
                    return "Đang giải quyết";
                }
                else
                {
                    return Convert.ToDateTime(ent_ChiTiet.NgaySuaChua).ToString("dd/MM/yyyy");
                }
            }
            else
            {
                return "Đang giải quyết";
            }
        }
    }
}